import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:htxd_flutter_mapbox_sdk/htxd_flutter_mapbox_sdk.dart';
import 'package:htxd_flutter_mapbox_sdk/mapbox/util/number_util.dart';

import 'land_flow_type_dialog.dart';

/// 确定
typedef SubmitDialog = Function(
    BuildContext context, LandInfoEntity landInfoEntity);

/// 取消
typedef CancelDialog = Function(BuildContext context);

/// 显示地块信息dialog
void showLandInfoCompleteDialog(
    {required BuildContext context,
    required LandInfoEntity landInfoEntity,
    required SubmitDialog submitDialog,
    required CancelDialog cancelDialog,
    String provinceCode = "",
    TaskType taskType = TaskType.PLANT}) {
  showDialog(
    barrierDismissible: false,
    context: context,
    builder: (context) {
      return WillPopScope(
          onWillPop: () async {
            return false;
          },
          child: LandInfoCompleteDialog(
            landInfoEntity: landInfoEntity,
            submitDialog: submitDialog,
            cancelDialog: cancelDialog,
            provinceCode: provinceCode,
            taskType: taskType,
          ));
    },
  );
}

class LandInfoCompleteDialog extends StatefulWidget {
  LandInfoEntity landInfoEntity;
  SubmitDialog submitDialog;
  CancelDialog cancelDialog;
  String provinceCode;
  TaskType taskType;

  LandInfoCompleteDialog(
      {Key? key,
      required this.landInfoEntity,
      required this.submitDialog,
      required this.cancelDialog,
      required this.provinceCode,
      required this.taskType})
      : super(key: key);

  @override
  State<LandInfoCompleteDialog> createState() => _LandInfoCompleteDialogState();
}

class _LandInfoCompleteDialogState extends State<LandInfoCompleteDialog> {
  late LandInfoEntity _landInfoEntity;

  late TextEditingController _landCertificateController;
  late TextEditingController _landNameController;
  late TextEditingController _confirmationAreaController;
  late TextEditingController _userNameController;
  late TextEditingController _idCardController;
  late TextEditingController _eastController;
  late TextEditingController _southController;
  late TextEditingController _westController;
  late TextEditingController _northController;

  @override
  void initState() {
    super.initState();
    _landInfoEntity = widget.landInfoEntity;
    _landCertificateController =
        TextEditingController(text: _landInfoEntity.landCertificate);
    _landNameController = TextEditingController(text: _landInfoEntity.landName);
    _confirmationAreaController =
        TextEditingController(text: _landInfoEntity.confirmationArea);
    _userNameController = TextEditingController(text: _landInfoEntity.userName);
    _idCardController = TextEditingController(text: _landInfoEntity.idCard);
    _eastController = TextEditingController(text: _landInfoEntity.east);
    _southController = TextEditingController(text: _landInfoEntity.south);
    _westController = TextEditingController(text: _landInfoEntity.west);
    _northController = TextEditingController(text: _landInfoEntity.north);
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        width: double.infinity,
        margin: EdgeInsets.symmetric(vertical: 10.w, horizontal: 32.w),
        padding: EdgeInsets.only(left: 15.w, right: 15.w, top: 15.w),
        decoration: BoxDecoration(
            color: Colors.white, borderRadius: BorderRadius.circular(10.w)),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Text(
              '完善地块信息',
              style: TextStyle(color: const Color(0xFF333333), fontSize: 16.sp),
            ),
            SizedBox(
              height: 10.w,
            ),
            Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Visibility(
                    visible: widget.provinceCode != "510000" &&
                        widget.taskType != TaskType.FARM,
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Container(
                          height: 40.w,
                          alignment: Alignment.center,
                          child: Row(
                            children: [
                              Expanded(
                                  child: Text.rich(TextSpan(children: [
                                TextSpan(
                                  text: '*',
                                  style: TextStyle(
                                      color: Colors.red, fontSize: 14.sp),
                                ),
                                TextSpan(
                                  text: '是否流转',
                                  style: TextStyle(
                                      color: const Color(0xFF333333),
                                      fontSize: 14.sp),
                                ),
                              ]))),
                              Expanded(
                                  child: GestureDetector(
                                onTap: () async {
                                  var isFlow = await showLandFlowTypeDialog(
                                      context: context);
                                  if (isFlow != null) {
                                    setState(() {
                                      _landInfoEntity.isFLow = isFlow;
                                    });
                                  }
                                },
                                child: Container(
                                  color: Colors.white,
                                  alignment: Alignment.centerRight,
                                  child: Row(
                                    mainAxisSize: MainAxisSize.min,
                                    children: [
                                      Text(
                                        widget.landInfoEntity.isFLow ?? "",
                                        style: TextStyle(
                                            color: const Color(0xFF333333),
                                            fontSize: 14.sp),
                                      ),
                                      Icon(
                                        Icons.keyboard_arrow_right,
                                        size: 20.w,
                                      )
                                    ],
                                  ),
                                ),
                              ))
                            ],
                          ),
                        ),
                        Container(
                          height: 1.w,
                          color: const Color(0xFFECECEC),
                        ),
                        Container(
                          height: 40.w,
                          alignment: Alignment.center,
                          child: Row(
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              Expanded(
                                  child: Text.rich(TextSpan(children: [
                                TextSpan(
                                  text: '*',
                                  style: TextStyle(
                                      color: Colors.red, fontSize: 14.sp),
                                ),
                                TextSpan(
                                  text: '确权地块编号',
                                  style: TextStyle(
                                      color: const Color(0xFF333333),
                                      fontSize: 14.sp),
                                ),
                              ]))),
                              Expanded(
                                child: Material(
                                  color: Colors.transparent,
                                  child: TextField(
                                    controller: _landCertificateController,
                                    textAlign: TextAlign.right,
                                    style: TextStyle(
                                        color: const Color(0xFF333333),
                                        fontSize: 14.sp),
                                    decoration: InputDecoration(
                                      border: InputBorder.none,
                                      hintText: "请输入",
                                      hintStyle: TextStyle(
                                          color: Colors.black38,
                                          fontSize: 14.sp),
                                    ),
                                  ),
                                ),
                              )
                            ],
                          ),
                        ),
                        Container(
                          height: 1.w,
                          color: const Color(0xFFECECEC),
                        ),
                        Container(
                          height: 40.w,
                          alignment: Alignment.center,
                          child: Row(
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              Expanded(
                                  child: Text.rich(TextSpan(children: [
                                TextSpan(
                                  text: '*',
                                  style: TextStyle(
                                      color: Colors.red, fontSize: 14.sp),
                                ),
                                TextSpan(
                                  text: '确权地块名称',
                                  style: TextStyle(
                                      color: const Color(0xFF333333),
                                      fontSize: 14.sp),
                                ),
                              ]))),
                              Expanded(
                                child: Material(
                                  color: Colors.transparent,
                                  child: TextField(
                                    controller: _landNameController,
                                    textAlign: TextAlign.right,
                                    style: TextStyle(
                                        color: const Color(0xFF333333),
                                        fontSize: 14.sp),
                                    decoration: InputDecoration(
                                      border: InputBorder.none,
                                      hintText: "请输入",
                                      hintStyle: TextStyle(
                                          color: Colors.black38,
                                          fontSize: 14.sp),
                                    ),
                                  ),
                                ),
                              )
                            ],
                          ),
                        ),
                        Container(
                          height: 1.w,
                          color: const Color(0xFFECECEC),
                        ),
                      ],
                    )),
                Container(
                  height: 40.w,
                  alignment: Alignment.center,
                  child: Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Expanded(
                          child: Text.rich(TextSpan(children: [
                        TextSpan(
                          text: '*',
                          style: TextStyle(color: Colors.red, fontSize: 14.sp),
                        ),
                        TextSpan(
                          text: widget.taskType == TaskType.PLANT
                              ? '承保地块面积'
                              : '饲养场所面积',
                          style: TextStyle(
                              color: const Color(0xFF333333), fontSize: 14.sp),
                        ),
                      ]))),
                      Expanded(
                        child: Material(
                          color: Colors.transparent,
                          child: Row(
                            children: [
                              Expanded(
                                child: TextField(
                                  controller: _confirmationAreaController,
                                  keyboardType:
                                      const TextInputType.numberWithOptions(
                                          decimal: true),
                                  inputFormatters: [
                                    FilteringTextInputFormatter.allow(
                                        RegExp(r'^\d*\.?\d*')),
                                  ],
                                  textAlign: TextAlign.right,
                                  style: TextStyle(
                                      color: const Color(0xFF333333),
                                      fontSize: 14.sp),
                                  decoration: InputDecoration(
                                    border: InputBorder.none,
                                    hintText: "请输入",
                                    hintStyle: TextStyle(
                                        color: Colors.black38, fontSize: 14.sp),
                                  ),
                                ),
                              ),
                              Text(
                                "亩",
                                style: TextStyle(
                                    color: const Color(0xFF333333),
                                    fontSize: 14.sp),
                              )
                            ],
                          ),
                        ),
                      )
                    ],
                  ),
                ),
              ],
            ),
            Container(
              height: 1.w,
              color: const Color(0xFFECECEC),
            ),
            Visibility(
              visible: _landInfoEntity.realityArea == null
                  ? false
                  : _landInfoEntity.realityArea!.isNotEmpty,
              child: Column(
                children: [
                  Container(
                    height: 40.w,
                    alignment: Alignment.center,
                    child: Row(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Expanded(
                            child: Text(
                          "地块勾画面积",
                          style: TextStyle(
                              color: const Color(0xFF333333), fontSize: 14.sp),
                        )),
                        Expanded(
                          child: Material(
                            color: Colors.transparent,
                            child: Row(
                              children: [
                                Expanded(
                                  child: Text(
                                    _landInfoEntity.realityArea == null
                                        ? ""
                                        : _landInfoEntity.realityArea!.isEmpty
                                            ? ""
                                            : NumberUtil.getValue(
                                                double.parse(_landInfoEntity
                                                    .realityArea!),
                                                2),
                                    textAlign: TextAlign.right,
                                    style: TextStyle(
                                        color: const Color(0xFF333333),
                                        fontSize: 14.sp),
                                  ),
                                ),
                                Text(
                                  "亩",
                                  style: TextStyle(
                                      color: const Color(0xFF333333),
                                      fontSize: 14.sp),
                                )
                              ],
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                  Container(
                    height: 1.w,
                    color: const Color(0xFFECECEC),
                  ),
                ],
              ),
            ),
            Container(
              height: 40.w,
              alignment: Alignment.center,
              child: Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Expanded(
                      child: Text.rich(TextSpan(children: [
                    TextSpan(
                      text: '*',
                      style: TextStyle(color: Colors.red, fontSize: 14.sp),
                    ),
                    TextSpan(
                      text: '被保险人名称',
                      style: TextStyle(
                          color: const Color(0xFF333333), fontSize: 14.sp),
                    ),
                  ]))),
                  Expanded(
                    child: Material(
                      color: Colors.transparent,
                      child: TextField(
                        controller: _userNameController,
                        textAlign: TextAlign.right,
                        style: TextStyle(
                            color: const Color(0xFF333333), fontSize: 14.sp),
                        decoration: InputDecoration(
                          border: InputBorder.none,
                          hintText: "请输入",
                          hintStyle:
                              TextStyle(color: Colors.black38, fontSize: 14.sp),
                        ),
                      ),
                    ),
                  )
                ],
              ),
            ),
            Container(
              height: 1.w,
              color: const Color(0xFFECECEC),
            ),
            Container(
              height: 40.w,
              alignment: Alignment.center,
              child: Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Expanded(
                      child: Text.rich(TextSpan(children: [
                    TextSpan(
                      text: '*',
                      style: TextStyle(color: Colors.red, fontSize: 14.sp),
                    ),
                    TextSpan(
                      text: '被保险人证件号码',
                      style: TextStyle(
                          color: const Color(0xFF333333), fontSize: 14.sp),
                    ),
                  ]))),
                  Expanded(
                    child: Material(
                      color: Colors.transparent,
                      child: TextField(
                        controller: _idCardController,
                        textAlign: TextAlign.right,
                        style: TextStyle(
                            color: const Color(0xFF333333), fontSize: 14.sp),
                        decoration: InputDecoration(
                          border: InputBorder.none,
                          hintText: "请输入",
                          hintStyle:
                              TextStyle(color: Colors.black38, fontSize: 14.sp),
                        ),
                      ),
                    ),
                  )
                ],
              ),
            ),
            Container(
              height: 1.w,
              color: const Color(0xFFECECEC),
            ),
            Container(
              height: 40.w,
              alignment: Alignment.center,
              child: Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Expanded(
                    child: Text(
                      '权证四至-东',
                      style: TextStyle(
                          color: const Color(0xFF333333), fontSize: 14.sp),
                    ),
                  ),
                  Expanded(
                    child: Material(
                      color: Colors.transparent,
                      child: TextField(
                        controller: _eastController,
                        textAlign: TextAlign.right,
                        style: TextStyle(
                            color: const Color(0xFF333333), fontSize: 14.sp),
                        decoration: InputDecoration(
                          border: InputBorder.none,
                          hintText: "请输入",
                          hintStyle:
                              TextStyle(color: Colors.black38, fontSize: 14.sp),
                        ),
                      ),
                    ),
                  )
                ],
              ),
            ),
            Container(
              height: 1.w,
              color: const Color(0xFFECECEC),
            ),
            Container(
              height: 40.w,
              alignment: Alignment.center,
              child: Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Expanded(
                    child: Text(
                      '权证四至-南',
                      style: TextStyle(
                          color: const Color(0xFF333333), fontSize: 14.sp),
                    ),
                  ),
                  Expanded(
                    child: Material(
                      color: Colors.transparent,
                      child: TextField(
                        controller: _southController,
                        textAlign: TextAlign.right,
                        style: TextStyle(
                            color: const Color(0xFF333333), fontSize: 14.sp),
                        decoration: InputDecoration(
                          border: InputBorder.none,
                          hintText: "请输入",
                          hintStyle:
                              TextStyle(color: Colors.black38, fontSize: 14.sp),
                        ),
                      ),
                    ),
                  )
                ],
              ),
            ),
            Container(
              height: 1.w,
              color: const Color(0xFFECECEC),
            ),
            Container(
              height: 40.w,
              alignment: Alignment.center,
              child: Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Expanded(
                    child: Text(
                      '权证四至-西',
                      style: TextStyle(
                          color: const Color(0xFF333333), fontSize: 14.sp),
                    ),
                  ),
                  Expanded(
                    child: Material(
                      color: Colors.transparent,
                      child: TextField(
                        controller: _westController,
                        textAlign: TextAlign.right,
                        style: TextStyle(
                            color: const Color(0xFF333333), fontSize: 14.sp),
                        decoration: InputDecoration(
                          border: InputBorder.none,
                          hintText: "请输入",
                          hintStyle:
                              TextStyle(color: Colors.black38, fontSize: 14.sp),
                        ),
                      ),
                    ),
                  )
                ],
              ),
            ),
            Container(
              height: 1.w,
              color: const Color(0xFFECECEC),
            ),
            Container(
              height: 40.w,
              alignment: Alignment.center,
              child: Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Expanded(
                      child: Text(
                    '权证四至-北',
                    style: TextStyle(
                        color: const Color(0xFF333333), fontSize: 14.sp),
                  )),
                  Expanded(
                    child: Material(
                      color: Colors.transparent,
                      child: TextField(
                        controller: _northController,
                        textAlign: TextAlign.right,
                        style: TextStyle(
                            color: const Color(0xFF333333), fontSize: 14.sp),
                        decoration: InputDecoration(
                          border: InputBorder.none,
                          hintText: "请输入",
                          hintStyle:
                              TextStyle(color: Colors.black38, fontSize: 14.sp),
                        ),
                      ),
                    ),
                  )
                ],
              ),
            ),
            Container(
              height: 1.w,
              color: const Color(0xFFECECEC),
            ),
            Row(
              children: [
                Expanded(
                  child: GestureDetector(
                    onTap: () {
                      _cancel();
                    },
                    child: Container(
                      height: 50.w,
                      color: Colors.white,
                      alignment: Alignment.center,
                      child: Text(
                        "取消",
                        style: TextStyle(color: Colors.red, fontSize: 14.sp),
                      ),
                    ),
                  ),
                ),
                Container(
                  width: 1.w,
                  height: 50.w,
                  color: const Color(0xFFECECEC),
                ),
                Expanded(
                  child: GestureDetector(
                    onTap: () {
                      _submit();
                    },
                    child: Container(
                      height: 50.w,
                      color: Colors.white,
                      alignment: Alignment.center,
                      child: Text(
                        "确定",
                        style: TextStyle(
                            color: const Color(0xFF007AFF), fontSize: 14.sp),
                      ),
                    ),
                  ),
                ),
              ],
            )
          ],
        ),
      ),
    );
  }

  // 取消
  void _cancel() {
    widget.cancelDialog.call(context);
  }

  // 提交
  void _submit() {
    String landCertificate = _landCertificateController.text;
    String landName = _landNameController.text;
    if (widget.provinceCode != "510000" && widget.taskType != TaskType.FARM) {
      if (landCertificate.isEmpty) {
        ToastUtil.showToast("请输入地块确权编号");
        return;
      }
      if (landName.isEmpty) {
        ToastUtil.showToast("请输入地块确权名称");
        return;
      }
    }

    String confirmationArea = _confirmationAreaController.text;
    if (confirmationArea.isEmpty || double.parse(confirmationArea) == 0.0) {
      if (widget.taskType == TaskType.FARM) {
        ToastUtil.showToast("请输入饲养场所面积");
      } else {
        ToastUtil.showToast("请输入承保地块面积");
      }
      return;
    }
    String userName = _userNameController.text;
    if (userName.isEmpty) {
      ToastUtil.showToast("请输入被保险人名称");
      return;
    }
    String idCard = _idCardController.text;
    if (idCard.isEmpty) {
      ToastUtil.showToast("请输入被保险人身份证号码");
      return;
    }
    String east = _eastController.text;
    String south = _southController.text;
    String west = _westController.text;
    String north = _northController.text;
    _landInfoEntity.landCertificate = landCertificate;
    _landInfoEntity.landName = landName;
    _landInfoEntity.confirmationArea = confirmationArea;
    _landInfoEntity.userName = userName;
    _landInfoEntity.idCard = idCard;
    _landInfoEntity.east = east;
    _landInfoEntity.south = south;
    _landInfoEntity.west = west;
    _landInfoEntity.north = north;
    widget.submitDialog.call(context, _landInfoEntity);
  }

  @override
  void dispose() {
    super.dispose();
    _landCertificateController.dispose();
    _landNameController.dispose();
    _confirmationAreaController.dispose();
    _userNameController.dispose();
    _idCardController.dispose();
    _eastController.dispose();
    _southController.dispose();
    _westController.dispose();
    _northController.dispose();
  }
}
